package com.ppgames.gm.gmsystem.auth;

import com.ppgames.gm.gmsystem.GmCore;
import com.ppgames.gm.gmsystem.controller.DemoController;
import com.ppgames.gm.gmsystem.util.DBHelper;
import com.ppgames.gm.gmsystem.util.MD5;
import com.ppgames.gm.gmsystem.util.Resp;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

import javax.persistence.EntityManager;
import javax.persistence.Query;

/**
 * TODO:写点说明把
 *
 * @author Joeg
 * @email 840459673@qq.com
 * @date 2018/4/19
 */
@Service
public class AuthService {


//    @Autowired
//    @Qualifier("entityManagerFactory")
    private EntityManager em;


    public Resp login(LoginReq loginVO) {

        String sql = "SELECT account,authority FROM  starwork_gm.t_user WHERE account='" + loginVO.getAccount() + "' AND password='" + MD5.GetMD5Code(loginVO.getPassword()).toLowerCase() + "' ";
        Query query = em.createNativeQuery(sql);
        LoginVO login = DBHelper.getOneCloum(query, LoginVO.class);
        if (login == null) {
            //登陆失败
            return Resp.customFail("1", "账号或密码错误");
        }
        return Resp.success(login);
    }


    public static class LoginVO {

        private String account;
        private Integer authority;


        public LoginVO() {
        }

        public LoginVO(String account, Integer authority) {
            this.account = account;
            this.authority = authority;
        }

        public String getAccount() {
            return account;
        }

        public void setAccount(String account) {
            this.account = account;
        }


        public Integer getAuthority() {
            return authority;
        }

        public void setAuthority(Integer authority) {
            this.authority = authority;
        }
    }
}
